package com.lopo.test;

import cn.hutool.core.map.MapUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;

public class Demo {
    public static void main(String[] args) {

        ArrayList<Student> list = new ArrayList<>();
        list.add(new Student(1, "a", 1));
        list.add(new Student(2, "b", 2));
        ArrayList<Student2> list2 = new ArrayList<>();

        list2.add(new Student2(3, "c", 3));
        ArrayList<Student3> list3 = new ArrayList<>();

        list3.add(new Student3(4, "d", 4));
        list3.add(new Student3(5, "d", 5));

        for (Student student : list) {
            final Function<Student, Map<String, String>> function = map.get(student.getType());
            if (function == null) continue;
            System.out.println("function.apply(student) = " + function.apply(student));
        }
    }


    private static Map<Integer, Function<Student, Map<String, String>>> map = new HashMap<>();

    static {
        map.put(1, Demo::One);
        map.put(2, Demo::Two);

    }

    private static void checkString(Student str, Predicate<Integer> pre, Function<Student, String> fun) {
        if (pre.test(str.getType())) {
            System.out.println("customShow(str,styleOne) = " + fun.apply(str));
        }
    }

    private static Map<String, String> One(Student s) {
        HashMap<String, String> map = new HashMap<>();
        map.put("title", "1111");
        map.put("content", "Name:" + s.getName() + " and Age:" + s.getAge());
        return map;
    }

    private static Map<String, String> Two(Student s) {
        HashMap<String, String> map = new HashMap<>();
        map.put("title", "222222");
        map.put("content", "Name:" + s.getName() + " and Age:" + s.getAge());
        return map;
    }

    public static Function<Student, String> styleOne = s -> {
        String result = "Name:" + s.getName() + " and Age:" + s.getAge();
        return result;
    };

    public static Function<Student, String> styleTwo = s -> {
        String result = "Name2:" + s.getName() + " and Age2:" + s.getAge();
        return result;
    };
}


@Data
@AllArgsConstructor
class Student {
    private Integer type;
    private String name;
    private int age;
}

@Data
@AllArgsConstructor
class Student2 {
    private Integer type;
    private String name;
    private int age;
}


@Data
@AllArgsConstructor
class Student3 {
    private Integer type;
    private String name;
    private int age;
}
